<template>
  <!-- 电表 -->
  <div class="bmsContent">
    <a-form :model="form" layout="inline" class="chartForm">
      <a-form-item>
        <a-radio-group v-model:value="form.chartType" @change="changeChartType">
          <a-radio-button
            v-for="item in dimensionList"
            :value="item.page"
            :key="item.page"
            >{{ item.name }}</a-radio-button
          >
        </a-radio-group>
      </a-form-item>
    </a-form>
    <a-spin :spinning="meterLoading">
      <div class="bmsBox" v-for="(item, index) in defaultData" :key="item">
        <div class="title">{{ item.data[0].linkName }}</div>
        <a-card class="content">
          <template #title>
            <div class="header">数据更新时间：{{ item.data[0].dateTime }}</div>
          </template>
          <div class="data">
            <div v-for="(dataItem, dataIndex) in item.data" :key="dataItem">
              {{ dataItem.name }}：<span v-param="dataItem.pdId">{{ dataItem.value }}{{ dataItem.unit }}</span>
            </div>
          </div>
        </a-card>
      </div>
      <a-empty
        v-if="!defaultData.length"
        style="opacity: 0.5; margin-top: 50px"
      />
    </a-spin>
  </div>
</template>
<script setup>
;
import { groupBy } from "lodash-es";
const props = defineProps({
  stationInfo: {
    type: Number,
  },
});
const form = ref({});
const defaultData = ref([]);
const $api = inject("$api");
const meterLoading = ref(false);
const getMeterRealtimeData = async () => {
  meterLoading.value = true;
  const res = await $api.meterRealtimeData({
    stationId: props.stationInfo.id,
    page: form.value.chartType,
  });
  meterLoading.value = false;
  defaultData.value = Object.entries(groupBy(res.data, "linkId")).map(
    ([key, value]) => {
      return {
        linkId: key,
        data: value,
      };
    }
  );
};
const dimensionList = ref([]);
const getMeterDimension = async () => {
  const res = await $api.meterDimension({
    stationId: props.stationInfo.id,
  });
  if (res?.code === 0) {
    dimensionList.value = res.data;
    form.value.chartType = dimensionList.value?.[0].page;
  }
};
const changeChartType = () => {
  getMeterRealtimeData();
}
const handleChageStation = async () => {
  await getMeterDimension();
  await getMeterRealtimeData();
};
defineExpose({ handleChageStation });
</script>

<style lang="less" scoped>
.bmsContent {
  overflow-y: auto;
  height: calc(100vh - 265px);
  width: 100%;
}
.bmsBox {
  width: 100%;
  min-height: 100px;
  .title {
    font-size: 20px;
    margin: 30px 0 12px 0;
  }
  .header {
    font-size: 16px;
    div {
      display: inline-block;
      width: 16%;
    }
  }
  .data {
    div {
      display: inline-block;
      width: 16%;
      margin-bottom: 24px;
      vertical-align: text-top;
    }
  }
}
::v-deep(.ant-card-body) {
  padding: 24px 24px 0 24px;
}
.chartForm {
  margin-top: 20px;
}
</style>